<template>
	<view class="withdraw-apply">
		<!-- 可用金额展示 -->
		<view class="available-amount">
			<text>可用金额: {{ availableAmount }}</text>
		</view>

		<!-- 提现表单 -->
		<view class="form">
			<!-- 提现金额输入 -->
			<view class="form-item">
				<text class="label">提现金额(元):</text>
				<input class="input" type="number" v-model="amount" placeholder="请输入提现金额" />
			</view>

			<!-- 收款微信号输入 -->
			<!-- <view class="form-item">
				<text class="label">收款微信号:</text>
				<input class="input" v-model="wechat" placeholder="请输入收款微信号" />
			</view> -->

			<!-- 备注说明 -->
			<view class="notes">
				<text class="note">1，提现金额不能大于可用金额。</text>
				<text class="note">2，需填写正确的收款微信号，默认为上次提现成功的微信号。</text>
				<text class="note">3，申请成功后自动转账到您微信号的零钱中。</text>
			</view>
		</view>

		<!-- 提交按钮 -->
		<button class="submit-btn" @click="handleSubmit">立即申请提现</button>
	</view>
</template>

<script>
	import {
		getWithdrawalInfoApi,
		postWithdrawalApi
	} from '@/api/user'
	export default {
		data() {
			return {
				availableAmount: '0',
				amount: '',
				wechat: '',
				minPrice: '0.01'
			}
		},
		onShow() {
			this.getInfo()
		},
		methods: {
			getInfo() {
				getWithdrawalInfoApi().then((res) => {
					if (res.status == 200) {
						this.availableAmount = res.data.storeAvailableAmount
						this.minPrice = res.data.minPrice
						this.amount = res.data.minPrice
					} else {
						uni.showToast({
							title: `${res.msg}`,
							icon: 'none'
						})
					}
				})
			},

			handleSubmit() {
				// 验证逻辑
				if (!this.amount) {
					uni.showToast({
						title: '请输入提现金额',
						icon: 'none'
					});
					return;
				}
				// if (!this.wechat) {
				// 	uni.showToast({
				// 		title: '请输入收款微信号',
				// 		icon: 'none'
				// 	});
				// 	return;
				// }
				if (parseFloat(this.amount) < parseFloat(this.minPrice)) {
					uni.showToast({
						title: '提现金额不能小于最小提现金额',
						icon: 'none'
					});
					return;
				}
				if (parseFloat(this.amount) > parseFloat(this.availableAmount)) {
					uni.showToast({
						title: '提现金额不能大于可用金额',
						icon: 'none'
					});
					return;
				}
				// 提交逻辑
				uni.showLoading({
					title: '提交中...'
				});
				postWithdrawalApi({
					"extractType": "weixin",
					"money": this.amount
				}).then((res) => {
					uni.hideLoading();
					if (res.status == 200) {
						uni.showToast({
							title: '申请提交成功'
						});
						setTimeout(() => {
							uni.navigateBack();
						}, 1500);
					}
				})



			}
		}
	}
</script>

<style lang="less">
	.withdraw-apply {
		padding: 30rpx;
		background-color: #fff;
		min-height: 100vh;

		.available-amount {
			font-size: 32rpx;
			color: #333;
			margin-bottom: 40rpx;
		}

		.form {
			.form-item {
				margin-bottom: 30rpx;

				.label {
					display: block;
					font-size: 28rpx;
					color: #666;
					margin-bottom: 10rpx;
				}

				.input {
					height: 80rpx;
					border: 1rpx solid #eee;
					border-radius: 8rpx;
					padding: 0 20rpx;
					font-size: 28rpx;
					background-color: #fff;
				}
			}

			.notes {
				margin-top: 40rpx;

				.note {
					display: block;
					font-size: 24rpx;
					color: #999;
					line-height: 1.6;
					margin-bottom: 10rpx;
				}
			}
		}

		.submit-btn {
			margin-top: 60rpx;
			background-color: #2169D3;
			color: #fff;
			font-size: 32rpx;
			height: 90rpx;
			line-height: 90rpx;
			border-radius: 45rpx;
		}
	}
</style>